package com.kkshop.admin;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.kkshop.utility.ApplicationContextConstants;
import com.kkshop.utility.DefaultingPropertyResourceBundle;

public class Main {

    private static final String PROPERTIES = "properties";

    public static void main(String[] args) {
        if (args.length < 1) {
            printUsage();
            System.exit(1);
        }

        Map<String, String> arguments = new HashMap<String, String>();
        arguments.put(PROPERTIES, null);

        SupportedCommand command;
        try {
            command = SupportedCommand.valueOf(args[0]);
        } catch (IllegalArgumentException e) {
            printUsage();
            System.exit(1);
            return;
        }

        int i = 1;
        while (i < args.length) {
            if (args[i].startsWith("-")) {
                String arg = args[i].substring(1);
                if (arguments.containsKey(arg) && (i + 1) < args.length
                        && !args[i + 1].startsWith("-")) {
                    arguments.put(arg, args[i + 1]);
                    i += 2;
                } else {
                    i += 1;
                }
            } else {
                ++i;
            }
        }

        if (arguments.containsValue(null)) {
            printUsage();
            System.exit(1);
            return;
        }

        try {
            FileInputStream fis = new FileInputStream(arguments.get(PROPERTIES));
            ResourceBundle properties = new DefaultingPropertyResourceBundle(fis);

            Enumeration<String> keys = properties.getKeys();
            while (keys.hasMoreElements()) {
                String key = keys.nextElement();
                System.setProperty(key, properties.getString(key));
            }

            AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                    ApplicationContextConstants.APPLICATION_CONTEXT_LOADER_PATTERN);

            int rc = command.executeCommand(applicationContext, properties);

            if (rc != 0) {
                System.exit(rc);
            }
        } catch (FileNotFoundException e) {
            System.err.println("Can not find properties file " + arguments.get(PROPERTIES));
            e.printStackTrace();
            System.exit(-1);
        } catch (IOException e) {
            System.err.println("Error reading properties file " + arguments.get(PROPERTIES));
            System.exit(-1);
        } catch (SecurityException e) {
            System.err.println("Error executing command " + e.getMessage());
            e.printStackTrace();
            System.exit(-1);
        }
    }

    private static void printUsage() {
        System.out
                .println("Usage: java -jar kkshop-admin.jar <command> -properties 'properties file'");
        System.out.println("valid <command> values are ");
        boolean first = true;
        for (SupportedCommand command : SupportedCommand.values()) {
            if (first) {
                first = false;
            } else {
                System.out.print(", ");
            }
            System.out.print(command);
        }
        System.out.println();
    }
}
